<template>
  <q-page class="q-pa-md markWrapBox">
    <div class="nav">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <el-image src="/svgs/publicMark.svg" style="width: 17px;margin-top: 2px" fit />
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span style=" color: #333;">快速入门</span></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="markContentBox" style="position: relative; ">
      <div class="whiteBg">
        <el-card class="box-card">
          <div class="contentBox">
            <div class="descBox">欢迎来到基因组功能模块机器学习算法开发和参数优化系统！
              <div class="btnBox">
                <!-- <el-button type="primary" @click="startSet">
                  <el-image class="imgTo" src="/svgs/quickIcon.svg" fit style="margin: 0 5px 0 -3px;" />
                  快速入门</el-button> -->
              </div>
            </div>
            <div class='titleSmallDesc'>在这里，您可以轻松地构建、训练和部署您的模型。以下是机器学习算法开发的主要流程：</div>
            <div class="flowBox">
              <div class="flowContent">
                <div class="oneData" v-for="(item, index) in flowData" :key="index">
                  <div v-if="item.title" :class="item.nowSelect ? 'model selectNow' : 'model'"
                    @click="item.nowSelect ? toPage(index) : ''">
                    <el-image class="elImg" :src="item.nowSelect ? item.selectIcon : item.icon" fit />
                    {{ item.title }}
                    <div class="loadDom" v-if="item.nowSelect && modelFlow">
                      <box-loading class="loadComponent" :noBgSet="true" />
                    </div>
                  </div>
                  <div class="arrows" v-if="index != flowData.length - 1 && item.title">
                    <el-image class="imgTo" src="/svgs/imgTo.svg" fit />
                  </div>
                  <transition name="el-zoom-in-top">
                    <div class="tipsBox" v-if="item.nowSelect && modelFlow && runNum > 10">训练作业运行较慢，请耐心等待</div>
                  </transition>
                </div>
              </div>
            </div>
            <div class="smallDesc" style="margin-top: 64px;">
              平台提供了“快速入门”来帮助AI初学者熟悉机器学习开发的主要流程，我们希望这个简单的引导可以帮助您快速上手使用我们的机器学习平台。</div>
            <div class="smallDesc">请点击“下一步”进行体验。</div>
          </div>
          <div class="quickBtnBox">
            <el-button type="info" plain @click="startSet('subtract')" v-show="store.quickStart.flowNumber > 1"
              :loading="loading || modelFlow">
              上一步</el-button>
            <el-button type="primary" @click="startSet('add')"
              v-show="store.quickStart.flowNumber <= 5 && store.quickStart.flowNumber != 0" :loading="modelFlow">
              下一步</el-button>
            <el-button type="primary" plain v-show="store.quickStart.flowNumber == 0">
              快速入门已完成</el-button>
          </div>
        </el-card>
      </div>
    </div>
  </q-page>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue";
import { useRouter } from 'vue-router';
import { dataSetStore } from 'src/stores/dataSet';
import { getQuickStartFlow, putQuickStartFlow } from 'src/api/project'
import { getAllRunsList } from 'src/api/model'
import boxLoading from 'src/components/boxLoading.vue';
import { ElMessage, ElMessageBox } from "element-plus";
const store = dataSetStore()
const router = useRouter()
const projectId = router.currentRoute.value.params.project_id
const flowData = ref([
  {},//因为和接口数据匹配，多写一个空对象
  {
    title: '新建数据集',
    icon: '/svgs/newSet.svg',
    selectIcon: '/svgs/newSetSelect.svg',
    nowSelect: false,
  },
  {
    title: '创建标注任务并标注',
    icon: '/svgs/createLabel.svg',
    selectIcon: '/svgs/createLabelSelect.svg',
    nowSelect: false

  },
  {
    title: '新增特征集',
    icon: '/svgs/feature.svg',
    selectIcon: '/svgs/featureSelect.svg',
    nowSelect: false
  },
  {
    title: '新建训练作业',
    icon: '/svgs/createWork.svg',
    selectIcon: '/svgs/createWorkSelect.svg',
    nowSelect: false
  },
  {
    title: '创建并部署模型',
    icon: '/svgs/createModel.svg',
    selectIcon: '/svgs/createModelSelect.svg',
    nowSelect: false
  }
])
const pagePathName = ref(['',
  'dataset',
  'dataAnnotations',
  'featureset',
  'model-manage',
  'model-app',
])
const loading = ref(false)
const modelFlow = ref(false)
const modelState = ref(<any>[])
const getState = ref()
const runNum = ref(0)
onMounted(() => {
  init()
  // putQuickStartFlow(projectId, { stage: 3 }).then(() => {
  //     init()
  //   })
})
function init() {
  loading.value = true
  getQuickStartFlow(projectId).then((res: any) => {
    if (res.data.stage > 0) {
      store.setQuickStart({ tipsPopup: false, quickFlow: true, flowNumber: res.data.stage, detailFlow: 1 })
    }
    flowData.value.forEach((item: any) => {
      item.nowSelect = false
    })
    flowData.value[store.quickStart.flowNumber].nowSelect = true
    loading.value = false
    if (store.quickStart.flowNumber == 5) {
      getModelState()
      runNum.value = 0
      getState.value = setInterval(() => {
        getModelState()
        runNum.value++
      }, 2000)
    }
  })
}
function startSet(typeBtn: string) {
  if (typeBtn == 'add') {
    toPage(store.quickStart.flowNumber)
  } else {
    putQuickStartFlow(projectId, { stage: store.quickStart.flowNumber - 1 }).then(() => {
      init()
    })
  }
}
function toPage(pathIndex: number) {
  if (pathIndex == 5) {
    if (modelState.value[0].status == 'success') {
      router.push({ name: pagePathName.value[pathIndex], params: { project_id: projectId } });
    } else {
      ElMessage.warning('新创建的训练作业正在运行中，请等待运行结束')
    }
  } else {
    router.push({ name: pagePathName.value[pathIndex], params: { project_id: projectId } });
  }
}
// 获取训练作业运行状态
function getModelState() {
  modelFlow.value = true
  modelState.value = []
  getAllRunsList(projectId, { title: store.quickModelTitle.title }).then((res: any) => {
    modelState.value = res.data.results.map((item: any) => {
      if (item.id == store.quickModelTitle.id) {
        return item
      }
    })
    if (modelState.value[0].status == 'success') {
      clearInterval(getState.value)
      modelFlow.value = false
      ElMessageBox.alert('新建训练作业运行成功，可以进行下一步啦', '提示', {
        confirmButtonText: '知道了',
        autofocus: false
      })
    }
    if (modelState.value[0].status == 'fail') {
      clearInterval(getState.value)
      ElMessage.error('训练作业运行失败，请重新创建')
      modelFlow.value = false
      putQuickStartFlow(projectId, { stage: store.quickStart.flowNumber - 1 }).then(() => {
        init()
      })
    }
  })


}
onUnmounted(() => {
  clearInterval(getState.value)
})
</script>

<style scoped lang="scss">
.markWrapBox {
  padding: 0 !important;

  .nav {
    display: flex;
    align-items: center;
    padding-left: 20px;
    cursor: pointer;
    background-color: var(--nav-bg-color);
    height: 46px;

    ::v-deep .el-breadcrumb {
      display: flex;
      align-items: center;
    }
  }

  .imgTo {
    width: 17px;
    display: flex;
    align-items: center;
    height: 100%;
  }

  .markContentBox {
    margin: 0 20px 20px 0;

    .quickBtnBox {
      display: flex;
      justify-content: end;
    }

    .whiteBg {
      background: white;
      height: calc(100vh - 180px);
      padding: 16px;
      border-radius: 4px;
    }

    .box-card {
      background: url(../../assets/quickStart/quickBg.jpg) 100% no-repeat;
    }

    .contentBox {
      width: 96%;
      margin: 0 auto;
      font-size: 16px;


      .descBox {
        font-size: 18px;
        font-weight: 500;
        margin: 5px 0 8px 0;
        position: relative;
      }

      .titleSmallDesc {
        font-size: 14px;
        font-weight: 400;
        margin-bottom: 8px;
        color: #666666;
      }

      .smallDesc {
        font-size: 14px;
        font-weight: 400;
        margin-bottom: 8px;

        &:last-child {
          color: #3D5CE4;
          font-weight: bold;
          margin-bottom: 80px;
        }
      }

      .flowBox {
        width: 100%;
        display: flex;
        margin: 60px auto 30px auto;

        .flowContent {
          display: flex;

          .oneData {
            display: flex;
            text-align: center;
            position: relative;

            .tipsBox {
              font-size: 13px;
              position: absolute;
              bottom: -25px;
              left: 10px;
              color: #ff7878;
            }

            .model {
              width: 200px;
              padding: 12px 0;
              background: white;
              border-radius: 8px;
              font-size: 15px;
              color: #333333;
              user-select: none;
              margin-right: 16px;
              position: relative;

              .loadDom {
                position: absolute;
                top: 0;
                width: 100%;
                height: 100%;
                background: rgb(255 255 255 / 60%);

                .loadComponent {
                  width: 100%;
                  height: 100%;
                  transform: scale(0.7);
                }
              }

              .elImg {
                width: 17px;
                margin: 3px 2px 0 0;
                vertical-align: middle;
              }
            }

            .selectNow {
              background: #3D5CE4;
              cursor: pointer;
              color: #FFF;

              &:hover {
                background: #5c79fb;
              }
            }

            .arrows {
              font-size: 26px;
              margin-right: 16px;
            }
          }
        }
      }

      .btnBox {
        display: flex;
        justify-content: flex-end;
        position: absolute;
        right: -20px;
        top: 0;
      }

    }
  }

}
</style>
